import { _decorator, AudioClip, Component, director, game, Node } from 'cc';
import { ScoreUI } from './UI/ScoreUI';
import { Player } from './Player';
import { GameOverUI } from './UI/GameOverUI';
import { AudioMgr } from './AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('GameManager')
export class GameManager extends Component {

    private static instance:GameManager = null;
    public static getInstance(){
        // 单例不能这么写，会报错
        // if (this.instance == null) {
        //     this.instance = new GameManager();
        // }
        return this.instance;
    }
    @property
    private bombNumber:number = 0;
    @property
    private score:number = 0;
    @property(ScoreUI)
    scoreUI:ScoreUI = null;

    @property(Player)
    player:Player = null;
    @property(Node)
    pauseBtnNode:Node = null;
    @property(Node)
    resumeBtnNode:Node = null;
    @property(GameOverUI)
    gameOverUI:GameOverUI = null;
    @property(AudioClip)
    gameMusice:AudioClip = null;
    @property(AudioClip)
    butAudio:AudioClip = null;
    @property(AudioClip)
    gameOverAudio:AudioClip = null;
    onLoad() {
        GameManager.instance = this;
    }
    protected start(): void {
        AudioMgr.inst.play(this.gameMusice,0.2);
    }
    update(deltaTime: number) {
        // console.log("==",deltaTime);
        
    }
    AddBomb(){
        this.bombNumber += 1;
        console.log("bombNumber",this.bombNumber);
        this.node.emit("onBombChange");
    }
    
    public GetBombNumber() : number {
        return this.bombNumber;
    }
    
    addScore(s:number){
        this.score += s;
        this.scoreUI.onBombChange(this.score);
    }
    // 暂停游戏
    onPauseBtnClick(){
        // game.pause();
        AudioMgr.inst.playOneShot(this.butAudio,0.1);
        AudioMgr.inst.pause();
        director.pause();
        this.player.disableCanControl( );
        this.pauseBtnNode.active = false;
        this.resumeBtnNode.active = true;
    }
    // 继续游戏
    onResumeBtnClick(){
        // game.resume();
        AudioMgr.inst.playOneShot(this.butAudio,0.1);
        AudioMgr.inst.resume();
        director.resume();
        this.player.enableControl( );
        this.pauseBtnNode.active = true;
        this.resumeBtnNode.active = false;

    }
    gameOver(){
        let hScore = localStorage.getItem("HighestScore");
        let hScoreInt = 0;
        if (hScore) {
            hScoreInt = parseInt(hScore,10);
        }
        let condition = this.score > hScoreInt;
        if (condition) {
            localStorage.setItem("HighestScore",this.score.toString());
        }
        this.gameOverUI.showGameOverUI(hScoreInt,this.score);
        this.onPauseBtnClick();
        AudioMgr.inst.playOneShot(this.gameOverAudio,0.2);
    }

    onRestartGameBtnClick() {
        AudioMgr.inst.playOneShot(this.butAudio,0.1);
        director.loadScene(director.getScene().name);
        this.onResumeBtnClick();
    }
    onQuitGameBtnClick() {
        
    }
    isHaveBomb():boolean{
        return this.bombNumber > 0;
    }
    useBomb(){
        this.bombNumber -= 1;
        this.node.emit("onBombChange");
    }
}

